package com.soft.javase.day14.fileupload;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * @author yzg
 * @desc TODO
 * @date 2025/5/24
 */
public class FileUploadServer {
    public static void main(String[] args) {
        try (
                ServerSocket serverSocket = new ServerSocket(8888)
        ) {
            System.out.println("服务器启动...");

            while (true) {
                Socket socket = serverSocket.accept();
                    try (
                            BufferedInputStream bis = new BufferedInputStream(socket.getInputStream());
                            BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("Java.png"))
                    ) {
                        byte[] b = new byte[8192];
                        int len;
                        while ((len = bis.read(b)) != -1) {
                            bos.write(b, 0, len);
                        }
                        System.out.println("文件已保存");
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}